package cn.bdqn.service.impl;

import cn.bdqn.exception.JxcException;
import cn.bdqn.dao.UserDao;
import cn.bdqn.entity.User;
import cn.bdqn.enums.JxcEnum;
import cn.bdqn.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 郑大仙丶
 * @date 2018-12-28 15:55
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;


    // 登录
    @Override
    public User login(String username, String password) {
        //1. 根据username查询用户信息
        User user = userDao.findByUsername(username);
        if(user == null){
            // 用户名错误.
            throw new JxcException(JxcEnum.USER_USERNAME_ERROR);
        }
        //2. 用户名正确,判断密码
        if(!StringUtils.equals(user.getPassword(),password)){
            // 密码不正确
            throw new JxcException(JxcEnum.USER_PASSWORD_ERROR);
        }
        //3. 登录成功
        return user;
    }
}
